<?php

namespace App\Kuafu\CouponExt\Channel\Xhs;

use App\Exceptions\BaseException;
use App\Kuafu\CouponExt\Channel\CouponExtBase;
use App\Kuafu\CouponExt\Channel\CouponExtBaseService;
use App\Kuafu\CouponExt\Enum\CouponExtCodeEnum;
use App\Kuafu\CouponExt\Enum\CouponExtEnum;
use App\Kuafu\CouponExt\Exceptions\CouponExtException;
use App\Remote\XiaoHongShu\XhsBusinessBaseRemote;
use App\Remote\XiaoHongShu\XhsCouponRemote;
use GuzzleHttp\Exception\GuzzleException;

/**
 * 小红书券码 详情｜核销｜撤销核销
 */
class XhsCouponBase extends CouponExtBaseService
{

    public function __construct($brandId = 0, $shopId = 0)
    {
        parent::__construct($brandId, $shopId);
    }

    /**
     * 券详情
     *
     * @param string $couponCode
     * @return array
     * @throws CouponExtException
     * @throws BaseException
     * @throws GuzzleException
     */
    public function couponPrepare(string $couponCode): array
    {
        // TODO: Implement couponPrepare() method.
        $couponDetail = XhsCouponRemote::couponInfo($couponCode);
        $responseCode = attributeGet($couponDetail, "code", null);
        $responseMsg  = attributeGet($couponDetail, "msg", "");

        switch ($responseCode) {
            case XhsBusinessBaseRemote::SUCCESS_CODE:
                //小红书券码信息成功响应
                $responseData       = $couponDetail["data"];
                $formatData         = parent::returnFormatData(
                    true,
                    $responseCode,
                    $responseMsg,
                    $responseData
                );
                $formatData["data"] = CouponExtBase::formatPrepareResponseData(
                    1,
                    1,
                    $responseData["id"],
                    jsonToArr($responseData["sku_info"])["sku_name"],
                    $responseData["coupon_code"],
                    CouponExtEnum::COUPON_TYPE_PRODUCT,
                    $responseData["pay_amount"],
                    $responseData["origin_amount"],
                    strtotime($responseData["start_time_text"]),
                    strtotime($responseData["expire_time_text"]),
                    CouponExtEnum::EXT_PLATFORM_XHS
                );
                return $formatData;
            default:
                //小红书券码信息错误响应
                throw new CouponExtException(
                    CouponExtCodeEnum::ERR_XHS_COUPON_PREPARE,
                    $responseMsg ? sprintf("小红书券码查询失败 【%s】", $responseMsg) : ""
                );
        }
    }

    /**
     * 券核销
     *
     * @param array|string $couponCode
     * @param int $count
     * @param string $orderId
     * @return array
     * @throws BaseException
     * @throws CouponExtException
     * @throws GuzzleException
     */
    public function couponConsume($couponCode, int $count, string $orderId): array
    {
        // TODO: Implement couponConsume() method.
        $verifyCouponResult = XhsCouponRemote::verifyCoupon($couponCode);
//        $verifyCouponResult = json_decode('{
//    "code": 1,
//    "msg": "ok",
//    "time": "1690355314",
//    "data": {
//        "result": 0,
//        "code": "K8ZR6M22HQP7",
//        "verify_id": "6903553379c77b0-7df9-4d3c-925e-57444a7c4bdb",
//        "order_id": "Pea690181636570175371",
//        "goods": {
//            "product_name": "测试0714123214",
//            "sold_start_time": "2023-07-14 00:00:00",
//            "sold_end_time": "2023-09-14 00:00:00",
//            "need_appointment": 0,
//            "days_of_week": "[\"7\"]",
//            "holidays": "[\"7\"]",
//            "sku_name": "11111111111",
//            "origin_amount": "1.600",
//            "actual_amount": "1.100",
//            "use_start_time_text": "",
//            "use_end_time_text": "",
//            "settle_type_text": ""
//        },
//        "order": {
//            "id": 20,
//            "order_id": "L724816364774527",
//            "xhs_order_id": "Pea690181636570175371",
//            "goodId": 30,
//            "poi_id": 193,
//            "sku_id": "64b123a98502590001367e60",
//            "verify_id": "6903553379c77b0-7df9-4d3c-925e-57444a7c4bdb",
//            "third_sku_id": "193_A10005",
//            "coupon_code": "K8ZR6M22HQP7",
//            "sku_info": "{\"sku_id\":\"64b123a98502590001367e60\",\"sku_name\":\"\\u6d4b\\u8bd50714\",\"third_sku_id\":\"193_A10005\"}",
//            "original_amount": 110,
//            "pay_amount": 110,
//            "origin_amount": 160,
//            "actual_amount": 110,
//            "phone": "6RubknstUqUhGYI7M3K65A==",
//            "start_time": 1689264000,
//            "expire_time": 1694707200,
//            "status": 2,
//            "send_coupon_time": 1690181642,
//            "verify_coupon_time": 1690355314,
//            "refund_time": null,
//            "create_time": 1690181636,
//            "update_time": 1690355314,
//            "start_time_text": "2023-07-14 00:00:00",
//            "expire_time_text": "2023-09-15 00:00:00",
//            "send_coupon_time_text": "2023-07-24 14:54:02",
//            "verify_coupon_time_text": "2023-07-26 15:08:34",
//            "create_time_text": "2023-07-24 14:53:56",
//            "update_time_text": "2023-07-26 15:08:34",
//            "status_text": null
//        }
//    }
//}', true);
        $responseCode = attributeGet($verifyCouponResult, "code", null);
        $responseMsg  = attributeGet($verifyCouponResult, "msg", "");

        switch ($responseCode) {
            case XhsBusinessBaseRemote::SUCCESS_CODE:
                //小红书验券信息成功响应
                $responseData       = $verifyCouponResult["data"];
                $formatData         = parent::returnFormatData(
                    true,
                    $responseCode,
                    $responseMsg,
                    $responseData
                );
                $formatData["data"] = CouponExtBase::formatConsumeResponseData(
                    [["couponCode" => $responseData["code"]]],
                    $responseData["order"]["id"],
                    jsonToArr($responseData["order"]["sku_info"])["sku_name"],
                    $responseData["order"]["origin_amount"]
                );
                return $formatData;
            default:
                //小红书券码信息错误响应
                throw new CouponExtException(
                    CouponExtCodeEnum::ERR_XHS_COUPON_CONSUME,
                    $responseMsg ? sprintf("小红书券码核销失败 【%s】", $responseMsg) : ""
                );
        }
    }

    /**
     * 券码撤销核销
     *
     * @param array $couponInfo
     * @return array
     * @throws BaseException
     * @throws CouponExtException
     * @throws GuzzleException
     */
    public function couponCancel(array $couponInfo): array
    {
        // TODO: Implement couponCancel() method.
        $cancelVerifyCouponResult = XhsCouponRemote::verifyCancelCoupon(
            $couponInfo["couponExtCode"],
            jsonToArr($couponInfo["couponExtCodeData"])["verify_id"]
        );
        $responseCode             = attributeGet($cancelVerifyCouponResult, "code", null);
        $responseMsg              = attributeGet($cancelVerifyCouponResult, "msg", "");

        switch ($responseCode) {
            case XhsBusinessBaseRemote::SUCCESS_CODE:
                //小红书核销券信息成功响应
                $responseData = $cancelVerifyCouponResult["data"];
                return parent::returnFormatData(
                    true,
                    $responseCode,
                    $responseMsg,
                    $responseData
                );
            default:
                //小红书撤销券码信息错误响应
                throw new CouponExtException(
                    CouponExtCodeEnum::ERR_XHS_COUPON_CANCEL,
                    $responseMsg ? sprintf("小红书券码撤销核销失败 【%s】", $responseMsg) : ""
                );
        }
    }

}
